using MediatR;
using Microsoft.AspNetCore.Mvc;
using SupplierCustomerManagement.Write.API.Applications.Commands.bankaccounts;
using B.S.RbacData.ErrorCode;
using System.ComponentModel.Design;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace SupplierCustomerManagement.Write.API.Controllers
{
    /// <summary>
    /// 银行账户管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class BankAccountsController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<BankAccountsController> _logger;

        public BankAccountsController(IMediator mediator, ILogger<BankAccountsController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增银行账户
        /// </summary>
        /// <param name="command">新增银行账户命令</param>
        /// <returns>新增结果</returns>
        [HttpPost]
        public async Task<ActionResult<ApiResult<int>>> AddBankAccount([FromBody] AddBankAccountCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);

                _logger.LogInformation("成功处理新增银行账户请求，关联实体ID: {RelatedEntityId}, 账户号码: {AccountNumber}", 
                    command.RelatedEntityId, command.AccountNumber);

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理新增银行账户请求时发生异常");
                
                var errorResult = new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"新增银行账户失败: {ex.Message}",
                    data = 0
                };

                return StatusCode(500, errorResult);
            }
        }

        /// <summary>
        /// 更新银行账户
        /// </summary>
        /// <param name="command">更新银行账户命令</param>
        /// <returns>更新结果</returns>
        [HttpPost]
        public async Task<ActionResult<ApiResult<bool>>> UpdateBankAccount([FromBody] UpdateBankAccountCommand command)
        {
            try
            {
                var result = await _mediator.Send(command);

                _logger.LogInformation("成功处理更新银行账户请求，银行账户ID: {BankAccountId}", command.Id);

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理更新银行账户请求时发生异常，银行账户ID: {BankAccountId}", command.Id);
                
                var errorResult = new ApiResult<bool>
                {
                    code = ResultEnumCode.fails,
                    message = $"更新银行账户失败: {ex.Message}",
                    data = false
                };

                return StatusCode(500, errorResult);
            }
        }

        /// <summary>
        /// 删除银行账户
        /// </summary>
        /// <param name="id">银行账户ID</param>
        /// <returns>删除结果</returns>
        [HttpPost]
        public async Task<ActionResult<ApiResult<bool>>> DeleteBankAccount([FromBody]DeleteBankAccountCommand command)
        {
            try
            {
                
                var result = await _mediator.Send(command);

                _logger.LogInformation("成功处理删除银行账户请求，银行账户ID: {BankAccountId}", command.Id);

                return Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "处理删除银行账户请求时发生异常，银行账户ID: {BankAccountId}", command.Id);
                
                var errorResult = new ApiResult<bool>
                {
                    code = ResultEnumCode.fails,
                    message = $"删除银行账户失败: {ex.Message}",
                    data = false
                };

                return StatusCode(500, errorResult);
            }
        }
    }
}